package com.zk.algorithm.string;

import com.zk.algorithm.annotation.Amazon;
import com.zk.algorithm.annotation.LeetCode;
import com.zk.algorithm.annotation.Medium;

/**
 * 版本号比较
 *
 * Input: version1 = "7.5.2.4", version2 = "7.5.3"
   Output: -1
 *
 * @author zk
 */
@LeetCode("165")
@Medium
@Amazon("https://www.nowcoder.com/discuss/148246?type=2&order=0&pos=74&page=1")
public class CompareVersionNumbers {

    /**
     * 按照 . 进行切割
     *
     * @param version1
     * @param version2
     * @return
     */
    public int compareVersion(String version1, String version2) {
        String[] levels1 = version1.split("\\.");
        String[] levels2 = version2.split("\\.");

        int length = Math.max(levels1.length, levels2.length);
        for (int i = 0; i < length; i++) {
            Integer v1 = i < levels1.length ? Integer.parseInt(levels1[i]) : 0;
            Integer v2 = i < levels2.length ? Integer.parseInt(levels2[i]) : 0;
            int compare = v1.compareTo(v2);

            if (compare != 0) {
                return compare;
            }
        }

        return 0;
    }

}
